package com.cbwl.eoas.web.framework.action;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component("eoasCorsFilter")  
public class EoasCORSFilter implements Filter { 
	
	private static Logger log = LoggerFactory.getLogger(EoasCORSFilter.class);
	
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
	  try {  
          HttpServletRequest httpRequest = (HttpServletRequest) request;  
          HttpServletResponse httpResponse = (HttpServletResponse) response;  

          // 跨域  
          String origin = httpRequest.getHeader("Origin");  
          if (origin == null) {  
              httpResponse.addHeader("Access-Control-Allow-Origin", "*");  
          } else {  
              httpResponse.addHeader("Access-Control-Allow-Origin", origin);  
          }  
          httpResponse.addHeader("Access-Control-Allow-Headers", "Origin, x-requested-with, Content-Type, Accept,X-Cookie");  
          httpResponse.addHeader("Access-Control-Allow-Credentials", "true");  
          httpResponse.setHeader("Access-Control-Max-Age", "3600");
         
          httpResponse.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS,DELETE");  
          if ( httpRequest.getMethod().equals("OPTIONS") ) {  
              httpResponse.setStatus(HttpServletResponse.SC_OK);  
              return;  
          }  
          chain.doFilter(request, response);  
      } catch (Exception e) {  
    	  log.error("Exception in crossDomainFilter.doFilter", e);  
          throw e;  
      }  
  }  
  public void init(FilterConfig filterConfig) {}  
  public void destroy() {}  
}